<?php


abstract class BaseCliente extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $nombre;


	
	protected $apellido_p;


	
	protected $apellido_m;


	
	protected $usuario;


	
	protected $password;


	
	protected $telefono;


	
	protected $email;


	
	protected $tipo = 'B';


	
	protected $fecha;


	
	protected $activo = true;

	
	protected $collRedsRelatedByClienteId;

	
	protected $lastRedRelatedByClienteIdCriteria = null;

	
	protected $collRedsRelatedByAgregado;

	
	protected $lastRedRelatedByAgregadoCriteria = null;

	
	protected $collVentas;

	
	protected $lastVentaCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getNombre()
	{

		return $this->nombre;
	}

	
	public function getApellidoP()
	{

		return $this->apellido_p;
	}

	
	public function getApellidoM()
	{

		return $this->apellido_m;
	}

	
	public function getUsuario()
	{

		return $this->usuario;
	}

	
	public function getPassword()
	{

		return $this->password;
	}

	
	public function getTelefono()
	{

		return $this->telefono;
	}

	
	public function getEmail()
	{

		return $this->email;
	}

	
	public function getTipo()
	{

		return $this->tipo;
	}

	
	public function getFecha($format = 'Y-m-d')
	{

		if ($this->fecha === null || $this->fecha === '') {
			return null;
		} elseif (!is_int($this->fecha)) {
						$ts = strtotime($this->fecha);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [fecha] as date/time value: " . var_export($this->fecha, true));
			}
		} else {
			$ts = $this->fecha;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getActivo()
	{

		return $this->activo;
	}

	
	public function setId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = ClientePeer::ID;
		}

	} 
	
	public function setNombre($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->nombre !== $v) {
			$this->nombre = $v;
			$this->modifiedColumns[] = ClientePeer::NOMBRE;
		}

	} 
	
	public function setApellidoP($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->apellido_p !== $v) {
			$this->apellido_p = $v;
			$this->modifiedColumns[] = ClientePeer::APELLIDO_P;
		}

	} 
	
	public function setApellidoM($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->apellido_m !== $v) {
			$this->apellido_m = $v;
			$this->modifiedColumns[] = ClientePeer::APELLIDO_M;
		}

	} 
	
	public function setUsuario($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->usuario !== $v) {
			$this->usuario = $v;
			$this->modifiedColumns[] = ClientePeer::USUARIO;
		}

	} 
	
	public function setPassword($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->password !== $v) {
			$this->password = $v;
			$this->modifiedColumns[] = ClientePeer::PASSWORD;
		}

	} 
	
	public function setTelefono($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->telefono !== $v) {
			$this->telefono = $v;
			$this->modifiedColumns[] = ClientePeer::TELEFONO;
		}

	} 
	
	public function setEmail($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->email !== $v) {
			$this->email = $v;
			$this->modifiedColumns[] = ClientePeer::EMAIL;
		}

	} 
	
	public function setTipo($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->tipo !== $v || $v === 'B') {
			$this->tipo = $v;
			$this->modifiedColumns[] = ClientePeer::TIPO;
		}

	} 
	
	public function setFecha($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [fecha] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->fecha !== $ts) {
			$this->fecha = $ts;
			$this->modifiedColumns[] = ClientePeer::FECHA;
		}

	} 
	
	public function setActivo($v)
	{

		if ($this->activo !== $v || $v === true) {
			$this->activo = $v;
			$this->modifiedColumns[] = ClientePeer::ACTIVO;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->nombre = $rs->getString($startcol + 1);

			$this->apellido_p = $rs->getString($startcol + 2);

			$this->apellido_m = $rs->getString($startcol + 3);

			$this->usuario = $rs->getString($startcol + 4);

			$this->password = $rs->getString($startcol + 5);

			$this->telefono = $rs->getString($startcol + 6);

			$this->email = $rs->getString($startcol + 7);

			$this->tipo = $rs->getString($startcol + 8);

			$this->fecha = $rs->getDate($startcol + 9, null);

			$this->activo = $rs->getBoolean($startcol + 10);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 11; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Cliente object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(ClientePeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			ClientePeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(ClientePeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = ClientePeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += ClientePeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collRedsRelatedByClienteId !== null) {
				foreach($this->collRedsRelatedByClienteId as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collRedsRelatedByAgregado !== null) {
				foreach($this->collRedsRelatedByAgregado as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collVentas !== null) {
				foreach($this->collVentas as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = ClientePeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collRedsRelatedByClienteId !== null) {
					foreach($this->collRedsRelatedByClienteId as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collRedsRelatedByAgregado !== null) {
					foreach($this->collRedsRelatedByAgregado as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collVentas !== null) {
					foreach($this->collVentas as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = ClientePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getNombre();
				break;
			case 2:
				return $this->getApellidoP();
				break;
			case 3:
				return $this->getApellidoM();
				break;
			case 4:
				return $this->getUsuario();
				break;
			case 5:
				return $this->getPassword();
				break;
			case 6:
				return $this->getTelefono();
				break;
			case 7:
				return $this->getEmail();
				break;
			case 8:
				return $this->getTipo();
				break;
			case 9:
				return $this->getFecha();
				break;
			case 10:
				return $this->getActivo();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = ClientePeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getNombre(),
			$keys[2] => $this->getApellidoP(),
			$keys[3] => $this->getApellidoM(),
			$keys[4] => $this->getUsuario(),
			$keys[5] => $this->getPassword(),
			$keys[6] => $this->getTelefono(),
			$keys[7] => $this->getEmail(),
			$keys[8] => $this->getTipo(),
			$keys[9] => $this->getFecha(),
			$keys[10] => $this->getActivo(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = ClientePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setNombre($value);
				break;
			case 2:
				$this->setApellidoP($value);
				break;
			case 3:
				$this->setApellidoM($value);
				break;
			case 4:
				$this->setUsuario($value);
				break;
			case 5:
				$this->setPassword($value);
				break;
			case 6:
				$this->setTelefono($value);
				break;
			case 7:
				$this->setEmail($value);
				break;
			case 8:
				$this->setTipo($value);
				break;
			case 9:
				$this->setFecha($value);
				break;
			case 10:
				$this->setActivo($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = ClientePeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setNombre($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setApellidoP($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setApellidoM($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setUsuario($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setPassword($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setTelefono($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setEmail($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setTipo($arr[$keys[8]]);
		if (array_key_exists($keys[9], $arr)) $this->setFecha($arr[$keys[9]]);
		if (array_key_exists($keys[10], $arr)) $this->setActivo($arr[$keys[10]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(ClientePeer::DATABASE_NAME);

		if ($this->isColumnModified(ClientePeer::ID)) $criteria->add(ClientePeer::ID, $this->id);
		if ($this->isColumnModified(ClientePeer::NOMBRE)) $criteria->add(ClientePeer::NOMBRE, $this->nombre);
		if ($this->isColumnModified(ClientePeer::APELLIDO_P)) $criteria->add(ClientePeer::APELLIDO_P, $this->apellido_p);
		if ($this->isColumnModified(ClientePeer::APELLIDO_M)) $criteria->add(ClientePeer::APELLIDO_M, $this->apellido_m);
		if ($this->isColumnModified(ClientePeer::USUARIO)) $criteria->add(ClientePeer::USUARIO, $this->usuario);
		if ($this->isColumnModified(ClientePeer::PASSWORD)) $criteria->add(ClientePeer::PASSWORD, $this->password);
		if ($this->isColumnModified(ClientePeer::TELEFONO)) $criteria->add(ClientePeer::TELEFONO, $this->telefono);
		if ($this->isColumnModified(ClientePeer::EMAIL)) $criteria->add(ClientePeer::EMAIL, $this->email);
		if ($this->isColumnModified(ClientePeer::TIPO)) $criteria->add(ClientePeer::TIPO, $this->tipo);
		if ($this->isColumnModified(ClientePeer::FECHA)) $criteria->add(ClientePeer::FECHA, $this->fecha);
		if ($this->isColumnModified(ClientePeer::ACTIVO)) $criteria->add(ClientePeer::ACTIVO, $this->activo);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(ClientePeer::DATABASE_NAME);

		$criteria->add(ClientePeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setNombre($this->nombre);

		$copyObj->setApellidoP($this->apellido_p);

		$copyObj->setApellidoM($this->apellido_m);

		$copyObj->setUsuario($this->usuario);

		$copyObj->setPassword($this->password);

		$copyObj->setTelefono($this->telefono);

		$copyObj->setEmail($this->email);

		$copyObj->setTipo($this->tipo);

		$copyObj->setFecha($this->fecha);

		$copyObj->setActivo($this->activo);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getRedsRelatedByClienteId() as $relObj) {
				$copyObj->addRedRelatedByClienteId($relObj->copy($deepCopy));
			}

			foreach($this->getRedsRelatedByAgregado() as $relObj) {
				$copyObj->addRedRelatedByAgregado($relObj->copy($deepCopy));
			}

			foreach($this->getVentas() as $relObj) {
				$copyObj->addVenta($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new ClientePeer();
		}
		return self::$peer;
	}

	
	public function initRedsRelatedByClienteId()
	{
		if ($this->collRedsRelatedByClienteId === null) {
			$this->collRedsRelatedByClienteId = array();
		}
	}

	
	public function getRedsRelatedByClienteId($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collRedsRelatedByClienteId === null) {
			if ($this->isNew()) {
			   $this->collRedsRelatedByClienteId = array();
			} else {

				$criteria->add(RedPeer::CLIENTE_ID, $this->getId());

				RedPeer::addSelectColumns($criteria);
				$this->collRedsRelatedByClienteId = RedPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(RedPeer::CLIENTE_ID, $this->getId());

				RedPeer::addSelectColumns($criteria);
				if (!isset($this->lastRedRelatedByClienteIdCriteria) || !$this->lastRedRelatedByClienteIdCriteria->equals($criteria)) {
					$this->collRedsRelatedByClienteId = RedPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastRedRelatedByClienteIdCriteria = $criteria;
		return $this->collRedsRelatedByClienteId;
	}

	
	public function countRedsRelatedByClienteId($criteria = null, $distinct = false, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(RedPeer::CLIENTE_ID, $this->getId());

		return RedPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addRedRelatedByClienteId(Red $l)
	{
		$this->collRedsRelatedByClienteId[] = $l;
		$l->setClienteRelatedByClienteId($this);
	}

	
	public function initRedsRelatedByAgregado()
	{
		if ($this->collRedsRelatedByAgregado === null) {
			$this->collRedsRelatedByAgregado = array();
		}
	}

	
	public function getRedsRelatedByAgregado($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collRedsRelatedByAgregado === null) {
			if ($this->isNew()) {
			   $this->collRedsRelatedByAgregado = array();
			} else {

				$criteria->add(RedPeer::AGREGADO, $this->getId());

				RedPeer::addSelectColumns($criteria);
				$this->collRedsRelatedByAgregado = RedPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(RedPeer::AGREGADO, $this->getId());

				RedPeer::addSelectColumns($criteria);
				if (!isset($this->lastRedRelatedByAgregadoCriteria) || !$this->lastRedRelatedByAgregadoCriteria->equals($criteria)) {
					$this->collRedsRelatedByAgregado = RedPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastRedRelatedByAgregadoCriteria = $criteria;
		return $this->collRedsRelatedByAgregado;
	}

	
	public function countRedsRelatedByAgregado($criteria = null, $distinct = false, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(RedPeer::AGREGADO, $this->getId());

		return RedPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addRedRelatedByAgregado(Red $l)
	{
		$this->collRedsRelatedByAgregado[] = $l;
		$l->setClienteRelatedByAgregado($this);
	}

	
	public function initVentas()
	{
		if ($this->collVentas === null) {
			$this->collVentas = array();
		}
	}

	
	public function getVentas($criteria = null, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collVentas === null) {
			if ($this->isNew()) {
			   $this->collVentas = array();
			} else {

				$criteria->add(VentaPeer::CLIENTE_ID, $this->getId());

				VentaPeer::addSelectColumns($criteria);
				$this->collVentas = VentaPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(VentaPeer::CLIENTE_ID, $this->getId());

				VentaPeer::addSelectColumns($criteria);
				if (!isset($this->lastVentaCriteria) || !$this->lastVentaCriteria->equals($criteria)) {
					$this->collVentas = VentaPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastVentaCriteria = $criteria;
		return $this->collVentas;
	}

	
	public function countVentas($criteria = null, $distinct = false, $con = null)
	{
				if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(VentaPeer::CLIENTE_ID, $this->getId());

		return VentaPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addVenta(Venta $l)
	{
		$this->collVentas[] = $l;
		$l->setCliente($this);
	}

} 